package 糖果传递;

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    static int N = 1000010;
    static long[] s = new long[N];
    static long[] c = new long[N];

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        for (int i = 1; i <= n; i++) {
            s[i] = in.nextInt();
            s[i] += s[i - 1];
        }
        long avg = s[n] / n;
        for (int i = 2; i <= n; i++) {
            c[i] = (i - 1) * avg - (s[i] - s[1]);
        }
        Arrays.sort(c, 1, n + 1);
        long res = 0, mid = c[(n + 1) / 2];
        for (int i = 1; i <= n; i++) {
            res += Math.abs(c[i] - mid);
        }
        System.out.println(res);
    }
}
